package com.babylove.inventory.controller;


import com.babylove.common.core.utils.poi.ExcelUtil;
import com.babylove.common.core.validate.Add;
import com.babylove.common.core.validate.Update;
import com.babylove.common.core.web.controller.BaseController;
import com.babylove.common.core.web.domain.AjaxResult;
import com.babylove.common.core.web.page.TableDataInfo;
import com.babylove.common.security.annotation.PreAuthorize;
import com.babylove.inventory.domain.InvStore;
import com.babylove.inventory.service.InvStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 仓库表 前端控制器
 * </p>
 *
 * @author caoqingwen
 * @since 2021-01-29
 */
@RestController
@RequestMapping("/store")
public class InvStoreController extends BaseController {


    @Autowired
    private InvStoreService storeService;

    /**
     * 获取仓库列表
     */
    @PreAuthorize(hasPermi = "inventory:store:list")
    @GetMapping("/list")
    public TableDataInfo list(InvStore store) {
        startPage();
        List<InvStore> list = storeService.selectStoreList(store);
        return getDataTable(list);
    }


    /**
     * 新增仓库
     *
     * @param store
     * @return
     */
    @PreAuthorize(hasPermi = "inventory:store:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated(Add.class) InvStore store) {
        return toAjax(storeService.addStore(store));
    }


    /**
     * 仓库详情
     *
     * @param id
     * @return
     */
    @PreAuthorize(hasPermi = "inventory:store:info")
    @GetMapping("/info")
    public AjaxResult info(@RequestParam("id") Long id) {
        return AjaxResult.success(storeService.selectInfo(id));
    }


    /**
     * 更新仓库
     *
     * @param store
     * @return
     */
    @PreAuthorize(hasPermi = "inventory:store:update")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody @Validated(Update.class) InvStore store) {
        return toAjax(storeService.updateStore(store));
    }


    /**
     * 删除仓库
     *
     * @param storeIds
     * @return
     */
    @PreAuthorize(hasPermi = "inventory:store:delete")
    @GetMapping("/{storeIds}")
    public AjaxResult delete(@PathVariable Long[] storeIds) {
        return toAjax(storeService.deleteStore(storeIds));
    }


    /**
     * 更新状态
     *
     * @param store
     * @return
     */
    @PreAuthorize(hasPermi = "inventory:store:update")
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody InvStore store) {
        return toAjax(storeService.changeStatus(store));
    }


    /**
     * 更新状态
     *
     * @param store
     * @return
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvStore store) throws IOException {
        List<InvStore> list = storeService.selectStoreList(store);
        ExcelUtil<InvStore> util = new ExcelUtil<InvStore>(InvStore.class);
        util.exportExcel(response, list, "仓库数据");
    }

}

